(function () {
     var _this;
	$.Encryption = {
		
		Cloud: {
			APP_ID: 'Pt2D4Gg2wfFgIaOGO263SRO1-gzGzoHsz',
			APP_KEY: 'EYr9FK4JLhsDNtLntbSRREj0'
		},
		Valine: {
			APP_ID: 'nbvwRDSIj9302hYN0p3EgCgT-gzGzoHsz',
			APP_KEY: 'XVOUM8qKVsqegg2TnN7sHpMQ'
		},
		MdData: {
			setChange(mdt) {
				console.log(mdt)
				mdt.cm.on('change', function () { //监听输入
					_this.MdData.bean.data=mdt.cm.getValue()
				})
			},
			creatMdBean(md,name,Identification){
               let result={
				   data:md,
				   name:name,
				   groupId:_this.CloudTable.MainCode.customer.groupId,
				   groupName:_this.CloudTable.MainCode.customer.groupName,
				   objectId:'',
				   UserId:_this.user.bean.name,
				   Identification:Identification,

			   }
			   return result
			},
			bean: {
				name: '',
				groupName: '',
				groupId: '',
				objectId: '',
				data: '',
			},


		},
	    getMdData(){
			return _this.MdData.bean
		},
		power:{
			AllowPostMd(){
				if(_this.MdData.bean.objectId==""){
					return true;
				}
               return (_this.power.havePower(_this.MdData.bean))
			},
			havePower(item){
				if(item.tableName==_this.CloudTable.MdList){
                    return _this.power.md.havePower(item)
				}else{
					return _this.power.number.havePower(item)
				}
			},
			md:{
				havePower(item){ //操作鉴权 比如修改删除
					if(_this.user.isMaster()||item.UserId==_this.user.bean.name){
						return true;
					}
					if(item.power!='0'&&isSameGroup(item.groupId)){ //是否为团队文件切文件权限为非个人读写
                        return true;
					}
					return false;
				},
				powerLab:[{name:'个人读写',value:'0'},{name:'团队读写',value:'1'},{name:'公开文档',value:'2'}]//个人权限:仅作者可写，团队可读不可写
			},
			number:{
				havePower(item){
					if(_this.user.isMaster()||_this.user.bean.power=='1'){
						return true;
					}
					if(isGroupOwer(item.groupId)){
						return true;
					}
					return false;
				},
				powerLab:[{name:'站长',value:'0'},{name:'超级管理员',value:'1'},{name:'会员',value:'2'}]
			}
		},
		CloudTable: {
			UserMaster: 'UserMaster',
			MdList: 'DbList',
			groupTable: 'groupTable',
			groupNumber: 'groupNumbers',
			MainCode: {
				customer: {
					groupName: '访客组',
					groupId: '5d4ba650c8959c008a7f7ce0',
					objectId: 'noneId'
				},
			}
		},
		user: {
			bean:{
				name: 'customer',
				objectId:'',
				power:'2',
			},
			groups:[],
			loginSucess(userBean) {
				_this.user.bean = userBean;
				saveCookie(_this.user.bean)
			},
			LoginOut: function () {
				_this.user.bean = {
					name:'customer'
				};
				delectCookie();
			},
			init: function () {
				_this=$.Encryption;
				var userCatch = readCookie()
				
				if (userCatch && userCatch != '' && userCatch != 'null') {
					_this.user.bean = JSON.parse(userCatch);
				}
			},
			isMaster:function(){
               if(_this.user.bean==null){
				return false;
			   }
               return _this.user.bean.power=='0'
			},
			do: function (callback) {
				callback(this.isLogin())
			},
			isLogin: function () {
				if (_this.user.bean.name == null||_this.user.bean.name =='customer') {
					return false;
				}
				let arg = _this.user.bean.name.trim();
				return (arg != null && arg != '')
			}
		}
	}
	function isSameGroup(groupId){
		// if(_this.CloudTable.MainCode.customer.groupId==item.groupId){
		// 	return true;
		// }
		return _this.user.groups.have('groupId',groupId);

	}
	function isGroupOwer(groupId){
		return false
		// _this.user.groups.getValueByColumnName()
	}
	var saveCookie = function (value) {
	$.cookie('userCookie',JSON.stringify(value), { expires: 7 });	
	}
	var readCookie = function () {
		return $.cookie('userCookie');
	}
	
	var delectCookie = function () {
		$.cookie('userCookie', null);
	}
})()

function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
                "SymbianOS", "Windows Phone",
                "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
	return flag;
}
